Walkthrough 9-3: Validate events

In this walkthrough, you use a validator to check if a query parameter called code with a value of SFO, LAX, CLE, PDX, or PDF is sent with a request and to throw an error if it is not. You will:

·       Add the Validation module to a project.

·       Use an Is true validator to check if a query parameter called code with a value of SFO, LAX, CLE, PDX, or PDF is sent with a request.

·       Return a custom error message if the condition is not met.

Starting file

If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.

Add the Validation module to the project

1.     Switch to the Mule Design perspective and return to implementation.xml.

2.     In the Mule Palette, select Add Modules.

3.     Select the Validation module in the right side of the Mule Palette and drag and drop it into the left side.

4.     If you get a Select module version dialog box, select the latest version and click Add.

 

Use the Is true validator to check for a valid destination code

5.     Locate the Is true validator in the right side of the Mule Palette.

6.     Drag and drop the Is true validator after the setCode Flow Reference in the getFlights flow.

7.     In the Is true properties view, set the display name to Is valid destination.

8.     Change the expression from False (Default) to Expression.

9.     Add a DataWeave expression to check if the code variable is one of the five destination codes.

#[['SFO','LAX','CLE','PDX','PDF'] contains vars.code]

Note: You can copy this expression from the course snippets.txt file.

10.  Using expression mode, set the error message to Invalid destination followed by the provided code.

'Invalid destination' ++ ' ' ++ (vars.code default ' ')

Note: You can copy this expression from the course snippets.txt file.

Debug the application

11.  Save the file then stop and debug the project.

12.  In Advanced REST Client, change the code to make a request to http://localhost:8081/flights?code=CLE.

13.  In the Mule Debugger, step past the validator; you should see the code is valid and application execution steps to the Choice router.

14.  Resume through the rest of the application.

15.  In Advanced REST Client, you should see flights for CLE.

16.  Change the code and make a request to http://localhost:8081/flights?code=FOO.

17.  In the Mule Debugger, step to the validator; you should see an error.

18.  Resume through the rest of the application; you should see your Invalid destination message in the console.

19.  Return to Advanced REST Client; you should get a 500 Server Error with your Invalid destination message.

Note: You will catch this error and send a JSON response with a different status code in the next module.

20.  Remove the code and make a request to http://localhost:8081/flights.

21.  In the Mule Debugger, step past the validator; you should not get any errors.

22.  Resume through the rest of the application.

23.  Return to Advanced REST Client; you should get a 200 response with all airline flights to SFO.

24.  Return to Anypoint Studio and switch to the Mule Design perspective.

Remove the default destination

25.  Locate the setCode subflow.

26.  In the Set Variable properties view, review the default value.

27.  Remove the default value from the value.

Debug the application

28.  Save the file to redeploy the project.

29.  In Advanced REST Client, make another request to http://localhost:8081/flights.

30.  In the Mule Debugger, step to the validator; you should now get an error.

31.  Resume through the rest of the application.

32.  Return to Advanced REST Client; you should get a 500 Server Error with your Invalid destination message.

Note: You will catch this error and send a JSON response with a different status code in the next module.

33.  Return to Anypoint Studio and stop the project.



Did you complete the walkthrough?

  Yes, I completed the walkthrough

  No, I did not complete the walkthrough

  I completed part of the walkthrough


Comments and/or feedback